//
//  ViewController.swift
//  TCToast
//
//  Created by 叮叮 on 2021/3/17.
//

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    var keyWindow: UIWindow? {
        get {
            var window:UIWindow? = nil
            if #available(iOS 13.0, *) {
                for windowScene:UIWindowScene in ((UIApplication.shared.connectedScenes as? Set<UIWindowScene>)!) {
                    if windowScene.activationState == .foregroundActive {
                        window = windowScene.windows.last
                        break
                    }
                }
                return window
            } else {
                return UIApplication.shared.keyWindow
            }
        }
    }
    
    var list = ["加载", "成功", "失败", "仅文本", "加载+成功", "加载+失败", "加载+仅文本", "加载+加载", "消失后再次弹窗", "展示在 KeyWindow上"]
    
    var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        title = "用例展示"
        
        tableView = UITableView(frame: view.bounds, style: .plain)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        view.addSubview(tableView)
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return list.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let identifier = "cell"
        let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath)
        cell.textLabel?.textAlignment = .center
        cell.textLabel?.text = list[indexPath.row]
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        switch indexPath.row {
        case 0:
            TCToast.show(in: self.view)
            DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
                TCToast.dismiss(in: self.view)
            }
        case 1:
            TCToast.show(in: self.view, type: .success)
        case 2:
            TCToast.show(in: self.view, type: .fail)
        case 3:
            TCToast.show(in: self.view, text: "仅文本")
        case 4:
            TCToast.show(in: self.view)
            DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
                TCToast.show(in: self.view, type: .success)  {
                    print("111")
                }
            }
        case 5:
            TCToast.show(in: self.view)
            DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
                TCToast.show(in: self.view, type: .fail)
            }
        case 6:
            TCToast.show(in: self.view)
            DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
                TCToast.show(in: self.view, text: "仅文本")
            }
        case 7:
            TCToast.show(in: self.view)
            DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
                TCToast.show(in: self.view, loadingText: "再次加载中...")
                DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
                    TCToast.dismiss(in: self.view)
                }
            }
        case 8:
            TCToast.show(in: self.view)
            DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
                TCToast.dismiss(in: self.view) {
                    TCToast.show(in: self.view, type: .success)
                }
            }
        case 9:
            if let keyWindow = keyWindow {
                TCToast.show(in: keyWindow)
                DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
                    TCToast.dismiss(in: keyWindow)
                }
            }
        default:
            print("nothing")
        }
    }
}
